package com.gjs.cd.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import com.gjs.common.enums.entity.UserOriginEnum;
import com.gjs.common.exception.BizException;
import com.gjs.common.manager.SmsVerifyCode;
import com.gjs.common.manager.notification.NotificationFailException;
import com.gjs.common.manager.response.RspBody;
import com.gjs.common.manager.sso.AccessTokenRsp;
import com.gjs.common.manager.sso.SsoClient;
import com.gjs.common.microservice.api.IUserMicroService;
import com.gjs.common.pojo.dto.user.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@Slf4j
@Api(tags = "光盘用户接口")
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    @DubboReference
    private IUserMicroService userMicroService;

    private final SsoClient ssoClient;

    private final SmsVerifyCode smsVerifyCode;

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public RspBody<UserInfoDTO> register(@RequestBody UserRegisterVO registerVO){
        // 校验短信验证码
        Assert.isTrue(smsVerifyCode.verify(SmsVerifyCode.VerifyType.REGISTER, registerVO.getMobile(), registerVO.getSmsVerificationCode()),"验证码错误或已过期");

        UserRegisterDTO userRegisterDto = BeanUtil.copyProperties(registerVO, UserRegisterDTO.class);
        // 设置用户来源
        userRegisterDto.setOrigin(UserOriginEnum.CD);
        // 调用微服务执行用户注册
        UserInfoDTO userInfoDTO = userMicroService.userRegister(userRegisterDto);

        return RspBody.success(userInfoDTO);
    }

    @PostMapping("/reset_password")
    @ApiOperation("用户重置密码")
    public RspBody resetPassword(@RequestBody UserResetPasswordVO resetPasswordVO){
        // 校验短信验证码
        Assert.isTrue(smsVerifyCode.verify(SmsVerifyCode.VerifyType.RESET_PASSWORD,resetPasswordVO.getMobile(),resetPasswordVO.getSmsVerificationCode()),"验证码错误或已过期");
        // 调用微服务执行用户重置密码
        Assert.isTrue(userMicroService.resetPassword(resetPasswordVO.getMobile(),resetPasswordVO.getPassword()),"重置用户密码失败");

        return RspBody.success("成功");
    }

    @PostMapping("/get_token_by_password")
    @ApiOperation("获取用户token")
    public RspBody<AccessTokenRsp> getTokenByPassword(@RequestBody UserLoginVO userLoginVO) throws IOException {
        String userAccount = userLoginVO.getUserAccount();
        Assert.notNull(userAccount,"无效账号");

        String password = userLoginVO.getPassword();
        Assert.notNull(password,"请输入密码");

        AccessTokenRsp accessTokenRsp = ssoClient.getAccessToken(userAccount,password,"all");
        return RspBody.success(accessTokenRsp);
    }

    @GetMapping("/send_register_valid_code")
    @ApiOperation("发送注册短信验证码")
    public RspBody sendRegisterValidCode(String mobile) throws NotificationFailException {
        // 校验是否有效手机号
        Validator.validateMobile(mobile,"无效手机号");
        // 判断手机号是否已注册过
        if(userMicroService.checkUserExistByMobile(mobile)){
            throw new BizException("该手机号已注册");
        }

        try{
            smsVerifyCode.send(SmsVerifyCode.VerifyType.REGISTER, mobile);
        }catch (Exception e){
            throw new NotificationFailException(e.getMessage());
        }

        return RspBody.success("成功");
    }

    @GetMapping("/send_reset_password_valid_code")
    @ApiOperation("发送重置密码短信验证码")
    public RspBody sendResetPasswordValidCode(String mobile) throws NotificationFailException {
        // 校验是否有效手机号
        Validator.validateMobile(mobile,"无效手机号");

        try{
            smsVerifyCode.send(SmsVerifyCode.VerifyType.RESET_PASSWORD, mobile);
        }catch (Exception e){
            throw new NotificationFailException(e.getMessage());
        }

        return RspBody.success("成功");
    }
}
